<template>
  <div class="cart-container">
    <!--    上面俩不动的购物车名-->
    <van-sticky>
      <div class="m-cartHd">
        <span class="logo">购物车</span>
        <span v-if="cartInfoList.length" class="right">编辑</span>
      </div>
    </van-sticky>
    <div v-if="cartInfoList.length" class="m-notice">
      自营仓商品，已满99元免邮
    </div>


    <!--    购物车列表-->
    <ul v-if="cartInfoList.length">
      <li class="cart-item" v-for="cartInfo in cartInfoList" :key="cartInfo.id">
        <van-swipe-cell>
          <!--       商品多选框小圆点-->
          <van-checkbox
            v-model="cartInfo.isChecked"
            checked-color="#ee0a24"
          ></van-checkbox>

          <!--       商品图片-->
          <img class="pic" :src="cartInfo.imgUrl" />
          <!--       商品整个信息-->
          <div class="sku-info">
            <!--         商品名称-->
            <p class="name">{{ cartInfo.skuName }}</p>
            <!--         商品价格-->
            <p class="price">¥{{ cartInfo.skuPrice }}</p>
            <!--         选择几个-->
            <van-stepper
              class="stepper"
              @plus="addNumber(cartInfo.skuId)"
              @minus="minusNumber(cartInfo.skuId)"
              v-model="cartInfo.skuNum"
              disable-input
            />
          </div>
          
          <template #right>
            <van-button
              square
              text="删除"
              type="danger"
              class="delete-button"
              @click="clearhandleDeleteOne(cartInfo.skuId)"
            />
          </template>

        </van-swipe-cell>
      </li>
    </ul>





    <!--    下面合计整体列表-->
    <div v-if="cartInfoList.length" class="m-cartFt">
      <van-checkbox
        class="select-all"
        @click="toggleSelect"
        :value="selectAll"
        checked-color="#ee0a24"
      />
      <span class="select-num">已选({{ selectNum }})</span>
      <span class="select-price">合计: ¥{{ totalPrice }}</span>
      <van-button class="create-order" type="danger" @click="toOrder"
        >下单</van-button
      >
    </div>
    <TabBar />
  </div>
</template>

<script>
import TabBar from "@/components/tabBar";
import {getCartData, updateCartNumber} from "@/api/Cart";

export default {
  name: 'CartYtATt',
  components: {TabBar},
  data() {
    return {
      cartInfoList: [],
      selectNum: null,
      selectAll: false,

    }
  },
  computed: {
    totalPrice() {
      let sum = 0
      let number = 0
      let flag = true;
      for (let i = 0; i < this.cartInfoList.length; i++) {
        if (this.cartInfoList[i].isChecked) {
          sum += this.cartInfoList[i].skuNum * this.cartInfoList[i].skuPrice;
          number += this.cartInfoList[i].skuNum;
        } else {
          flag = false;
        }
      }
      this.selectNum = number;
      this.selectAll = flag;
      return sum;
    },
  },
  created() {
    console.log("进入购物车组件")
    getCartData().then((response) => {
      //获取购物车列表数据
      this.cartInfoList = response.data[0].cartInfoList
      console.log(this.cartInfoList)
    })
  },
  methods: {
      /*

        这儿！！！

       */ 
     clearhandleDeleteOne(skuId) {
      //删除当前数据
  
        //获取删除接口
        
        //重新获取数据
        

    },
    toggleSelect() {
      if (this.selectAll) {
        this.cartInfoList.forEach(cartInfo => cartInfo.isChecked = 0);
      } else {
        this.cartInfoList.forEach(cartInfo => cartInfo.isChecked = 1);
      }
    },
    addNumber(skuId) {
      updateCartNumber(skuId, 1).then((response) => {
        console.log(response);
      });

    },
    minusNumber(skuId) {
      updateCartNumber(skuId, -1).then((response) => {
        console.log(response);
      });
    },
    // 点击下单跳转到 订单页面
    toOrder(){
      this.$router.push('/order')
    }
  }
}
</script>

<style lang="less" scoped>
p {
  margin: 0;
}

.cart-container {
  background-color: rgb(238, 238, 238);
  padding-bottom: 110px;
}

.m-cartHd {
  height: 44px;
  padding: 0 15px;
  line-height: 44px;
  background-color: #ffffff;
  border-bottom: 1px solid #c7c7c7;

  .logo {
    float: left;
    font-size: 18px;
  }

  .right {
    font-size: 15px;
    float: right;
  }
}

.m-notice {
  padding: 0 15px;
  line-height: 40px;
  font-size: 15px;
  color: #f48f18;
  background-color: #fff8d8;
  margin-bottom: 10px;
}

.cart-item {
  background-color: white;
  padding: 12px 15px;
  margin-bottom: 10px;
  position: relative;
  .van-swipe-cell {
    display: flex;
    .van-checkbox {
      width: 20px;
      height: 20px;
    }
    .pic {
      width: 86px;
      height: 86px;
      margin-right: 10px;
      border-radius: 4px;
      background-color: #eeeeee;
    }

    .sku-info {
      flex: 1;
      height: 86px;
      position: relative;

      .name {
        max-height: 36px;
        line-height: 18px;
        overflow: hidden;
        text-overflow: ellipsis;
        word-break: break-all;
        font-size: 14px;
      }

      .price {
        font-family: PingFangSC-Medium;
        font-size: 14px;
        color: #333333;
        position: absolute;
        left: 0;
        bottom: 0;
      }

      .stepper {
        position: absolute;
        right: 0;
        bottom: 0;
      }
    }

    .delete-button {
      height: 200px;
    }
  }
}

.m-cartFt {
  background-color: white;
  border-top: 1px solid #c7c7c7;
  border-bottom: 1px solid #c7c7c7;
  position: fixed;
  bottom: 50px;
  width: 100%;
  height: 60px;
  display: flex;
  align-items: center;

  .select-all {
    margin-left: 15px;
    margin-right: 8px;
  }

  .select-num {
    color: #7f7f7f;
    font-size: 14px;
  }

  .select-price {
    overflow: hidden;
    word-wrap: normal;
    white-space: nowrap;
    text-overflow: ellipsis;
    max-width: 150px;
    font-size: 14px;
    color: #dd1a21;
    position: absolute;
    right: 120px;
  }

  .create-order {
    width: 113px;
    height: 100%;
    border-radius: 0;
    position: absolute;
    right: 0;
  }
}


</style>
